package com.wwwarehouse.resource_center.fragment.rules;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.bean.rules.TaskUnitBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.eventbus_event.Event;
import com.wwwarehouse.resource_center.eventbus_event.RulesEvent;
import com.wwwarehouse.resource_center.fragment.base.MyBaseViewPagerFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/8/25.
 *         <p/>
 *         //任务
 */

public class SelectTaskParentFragment extends MyBaseViewPagerFragment {

    ArrayList<TaskUnitBean.DataBean.ListBean> mSelectListBean ;
    ArrayList<TaskUnitBean.DataBean.ListBean> mListBean = new ArrayList<>();
    int mNum = 27;
    public static final int GET_DISTRIBUT_ABLE_UNITS = 1;  //获取可分配的任务
    private String businessId;

    @Override
    protected boolean isRegisterEventBus() {
        return true;
    }

    @Override
    protected void loadDatas() {
        mConfirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //确认
                EventBus.getDefault().post(new RulesEvent(RulesEvent.RULE_TASK,mListBean,"RULE_TASK"));
                popFragment();
            }
        });
        Bundle bundle= getArguments();
        if (bundle!=null){
            mSelectListBean = bundle.getParcelableArrayList("mSelectTasksListBean");
        }
        if (mSelectListBean!=null&&mSelectListBean.size()>0){
            mListBean.addAll(mSelectListBean);
            mActivity.setTitle("选择任务类型("+mListBean.size()+")");
            mConfirmBtn.setVisibility(View.VISIBLE);
        }else {
            mActivity.setTitle("选择任务类型");
        }
        businessId = bundle.getString("businessId");
    }

    @Override
    public void requestDatas() {
        Map<String, Object> map = new HashMap<>();
        map.put("page",1);
        map.put("size",1);
        map.put("sort","UPDATE_TIME DESC");
        httpPost(ResourceConstant.GET_DISTRIBUT_ABLE_UNITS,map,GET_DISTRIBUT_ABLE_UNITS,false,"");

    }
    @Override
    protected void receiveEvent(Event event) {
        if (event.getCode()==RulesEvent.RULE_SELECT_TASK){
            TaskUnitBean.DataBean.ListBean  bean = (TaskUnitBean.DataBean.ListBean) event.getData();
            if (null!=bean){
                LogUtils.showInfoLog("notify:"+bean.toString());
                //可能同一个，一个是true，一个是false
                if (!mListBean.contains(bean)){
                    if (mListBean.size()>0){
                        for (int i = 0; i < mListBean.size(); i++) {
                            if (mListBean.get(i).getAcitonUnitType().equals(bean.getAcitonUnitType())&&!bean.isSelect()){
                                mListBean.remove(i);
                                break;
                            }
                            if (!mListBean.get(i).getAcitonUnitType().equals(bean.getAcitonUnitType())&&bean.isSelect()){
                                mListBean.add(bean);
                                break;
                            }
                        }
                    }else {
                        mListBean.add(bean);
                    }
                }else {
                    if (!bean.isSelect()){
                        mListBean.remove(bean);
                    }
                }
            }
            //                int selectTagCount = getSelectTagCount();
            int selectTagCount = mListBean.size();
            mConfirmBtn.setVisibility(selectTagCount>0? View.VISIBLE:View.GONE);
            LogUtils.showInfoLog("selectTagCount:"+selectTagCount);
            if (selectTagCount>0){
                mActivity.setTitle("选择任务类型("+selectTagCount+")");
            }else {
                mActivity.setTitle("选择任务类型");
            }
        }
    }


    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what){
            case GET_DISTRIBUT_ABLE_UNITS:
                if (TextUtils.equals("0", commonClass.getCode())){
                    TaskUnitBean.DataBean dataBean = JSON.parseObject(commonClass.getData().toString(), TaskUnitBean.DataBean.class);
                    int total = dataBean.getTotal();
                    if (total<=0){
                        mLoadingView.setVisibility(View.VISIBLE);
                        mLoadingView.showEmptyView(true);
                        mLoadingView.setEmptyListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mLoadingView.showProgressView(true);
                                requestDatas();
                            }
                        });
                        return;
                    }
                    Bundle  bundle = new Bundle();
                    bundle.putParcelableArrayList("mSelectTasksListBean",mListBean);
                    SetFragmentAndPage(total,mNum,new SelectTaskChildFragment().getClass().getName(),bundle);
                }
                break;
        }
    }

    @Override
    public void onReLoad(int code) {

    }
}
